﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using ASBCC.Database.Entity;
using SourceCode.Workflow.Client;
using ASBCC.Database.Service;

namespace ASBCC.Workflow.K2Manager
{
    public class ResubmitActionHandler : ActionHandlerBase
    {
        #region Constructor

        public ResubmitActionHandler(SerializableDictionary dataFields, K2AppForm appForm, K2WorkItem workItem, K2ActionInfo actionInfo, string comment, K2AuditDestination auditDestination, List<K2User> outputDestinationUsers) :
            base(dataFields, appForm, workItem, actionInfo, comment, auditDestination, outputDestinationUsers)
        {
        }

        #endregion

        #region Private Method

        private List<MyActivityTableEntity> GetMyActivityListTobeMoved_When_Resubmitting()
        {
            List<MyActivityTableEntity> myActivityList = new List<MyActivityTableEntity>();
            foreach (MyActivityTableEntity myActivity in _myActivitySetBeforeMoving)
            {
                if (string.Compare(myActivity.K2ActivityName, K2ActivityName.Submission) == 0 && string.Compare(WorkItem.Destination, myActivity.K2Destination) != 0)
                {
                    myActivityList.Add(myActivity);
                }
            }
            return myActivityList;
        }

        #endregion

        #region Override Methods

        protected override void BuildMyActivity()
        {
            //获取这个Folio下所有的WorklistItem，包含Action动作之后产生的WorklistItem，也有可能包含已有的WorklistItem
            _allWorklistItemsAfterAction = K2Admin.GetWorklistItems(ProcessName, AppForm.Folio);
            //获取所有ProcInstID的MyActivity
            //用于Merge的MyActivity必须满足状态为0:正常 以及 1:挂起(有可能出现AskForMore或ForwordBack后,所以挂起状态的待办也可能用于Merge)
            _myActivitySetBeforeMoving = K2DataHelper.GetMyActivities(ProcInstID, new int[] { (int)MyActivityStatus.Normal, (int)MyActivityStatus.Suspended });
            MergeWorklistItemsAndMyActivitySet();

            List<MyActivityTableEntity> tobeMoved = GetMyActivityListTobeMoved_When_Resubmitting();
            foreach (MyActivityTableEntity myActivity in tobeMoved)
            {
                _myActivityListToBeMoved.Add(myActivity);
            }

            MoveMyActivityList();
            CreateNewMyActivityList();
        }

        /// <summary>
        /// Resubmit动作时,重载图章生成方法,不需要生成图章
        /// </summary>
        protected override void BuildProcessStamp()
        {
            //K2DataHelper.ClearDisabledProcessStamp(WorkItem.ProcInstId);
            K2DataHelper.DisableProcessStamp(WorkItem.ProcInstId);

        }

        protected override void BuildProcessComment()
        {
            K2DataHelper.ClearDisabledProcessComment(WorkItem.ProcInstId);
            base.BuildProcessComment();
        }

        protected override void SendMail()
        {
            string actionType = GetActionType();
            string ApplicationNameEn = FlowProcess.ProcDisplayEn;
            string ApplicationNameCn = FlowProcess.ProcDisplayCn;

            Flow_ProcessMailConfigTableEntity mailConfig = K2DataHelper.GetProcessMailConfig(WorkItem.ProcName, WorkItem.ActivityName, actionType);
            if (mailConfig == null) return;

            EZVisitApplicationTableEntity entity = EZVisitApplicationManager.SelectByKeys(AppForm.FormId);
            if (entity == null) return;

            // 通知下一步审批人
            if (NewMyActivityList != null && NewMyActivityList.Count > 0)
            {
                foreach (MyActivityTableEntity myActivity in NewMyActivityList)
                {
                    if (myActivity.OwnerID == K2User.TempUser) continue; //临时用户不需要通知

                    int toAuditorTemplate = mailConfig.ToAuditorTemplate ?? 0;
                    if (toAuditorTemplate <= 0) continue;

                    EmployeeTableEntity ownerInfo = K2DataHelper.GetUserInfoById(myActivity.OwnerID ?? 0);
                    if (!CheckMailApproverRule(ownerInfo)) continue;

                    string[] mailAddressArr = new string[] { ownerInfo.Email };
                    NameValueCollection collection = new NameValueCollection();
                    collection["{EnglishName}"] = myActivity.OwnerEn;
                    collection["{ChineseName}"] = myActivity.OwnerCn;
                    if (!entity.EZVisitReqNoIsNull())
                        collection["{SerialNumber}"] = entity.EZVisitReqNo;
                    else
                        collection["{SerialNumber}"] = myActivity.ApplicationNo;
                    collection["{SubmitterEN}"] = entity.RequesterNameEN;
                    collection["{SubmitterCN}"] = entity.RequesterNameCN;
                    collection["{FormURL}"] = myActivity.FormURL;

                    NameValueCollection titleCollection = new NameValueCollection();
                    if (entity.EZVisitReqNoIsNull())
                        titleCollection["{SerialNumber}"] = entity.EZVisitReqNo;
                    else
                        titleCollection["{SerialNumber}"] = myActivity.ApplicationNo;
                    MailService.MailSender(mailAddressArr, toAuditorTemplate, collection, titleCollection);
                }
            }
        }

        protected override string GetActionType()
        {
            return K2ActionType.Resubmit;
        }

        #endregion
    }
}
